package com.google.gwt.it2tool.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

public class ClientDataCache {
	
	private static ClientDataCache instance;
	
	private ArrayList<DataChangeHandler> changeHandlers = new ArrayList<DataChangeHandler>();
	
	private HashMap<String,Factory>  factories = new HashMap<String,Factory> ();
	private HashMap<String,Recipe>  recipes = new HashMap<String,Recipe> ();
	private HashMap<String,ProductionPrototype>  prototypes = new HashMap<String,ProductionPrototype> ();
	private CustomFactoryOrder customFactoryOrder = new CustomFactoryOrder();
	private int massProdLevel = 0;
	
	private ClientDataCache(){
		super();
	}
	
	public static ClientDataCache getInstance(){
		if(instance==null){
			instance = new ClientDataCache();
		}
		return instance;
	}
	
	public void addDataChangeHandler(DataChangeHandler handler){
		changeHandlers.add(handler);
	}
	
	public boolean removeDataChangeHandler(DataChangeHandler handler){
		return changeHandlers.remove(handler);
	}
	
	private void triggerOnFactoryDataChanged(){
		int size = changeHandlers.size();
		for (int i=0;i<size;i++){
			changeHandlers.get(i).onFactoryDataChanged();
		}
	}
	
	private void triggerOnRecipeDataChanged(){
		int size = changeHandlers.size();
		for (int i=0;i<size;i++){
			changeHandlers.get(i).onRecipeDataChanged();
		}
	}
	
	private void triggerOnPrototypeDataChanged(){
		int size = changeHandlers.size();
		for (int i=0;i<size;i++){
			changeHandlers.get(i).onPrototypeDataChanged();
		}
	}
	
	private void triggerOnMassProductionDataChanged(){
		int size = changeHandlers.size();
		for (int i=0;i<size;i++){
			changeHandlers.get(i).onMassProductionDataChanged();
		}
	}

	public HashMap<String, Factory> getFactories() {
		return factories;
	}

	public void setFactories(HashMap<String, Factory> factories) {
		this.factories = factories;
		triggerOnFactoryDataChanged();
	}
	
	public void addFactory(Factory f){
		factories.put(f.getId(),f);
		triggerOnFactoryDataChanged();
	}
	
	public boolean removeFactory(Factory f){
		boolean ret = (factories.remove(f.getId())!=null);
		if (ret) triggerOnFactoryDataChanged();
		return ret;
	}

	public HashMap<String, Recipe> getRecipes() {
		return recipes;
	}

	public void setRecipes(HashMap<String, Recipe> recipes) {
		this.recipes = recipes;
		triggerOnRecipeDataChanged();
	}
	
	public void addRecipe(Recipe r){
		recipes.put(r.getName(),r);
		triggerOnRecipeDataChanged();
	}
	
	public boolean removeRecipe(Recipe r){
		boolean ret = (recipes.remove(r.getName())!=null);
		if(ret) triggerOnRecipeDataChanged();
		return ret;
	}

	public HashMap<String, ProductionPrototype> getPrototypes() {
		return prototypes;
	}

	public void setPrototypes(HashMap<String, ProductionPrototype> prototypes) {
		this.prototypes = prototypes;
		triggerOnPrototypeDataChanged();
	}
	
	public void addPrototype(ProductionPrototype p){
		prototypes.put(p.getName(),p);
		triggerOnPrototypeDataChanged();
	}
	
	public boolean removePrototype(ProductionPrototype p){
		boolean ret = (prototypes.remove(p.getName())!=null);
		if (ret) triggerOnPrototypeDataChanged();
		return ret;
	}

	public int getMassProdLevel() {
		return massProdLevel;
	}
	
	public void setCustomFactoryOrder(CustomFactoryOrder order){
		customFactoryOrder = order;
	}
	
	public CustomFactoryOrder getCustomFactoryOrder(){
		return customFactoryOrder;
	}

	public void setMassProdLevel(int massProdLevel) {
		this.massProdLevel = massProdLevel;
		triggerOnMassProductionDataChanged();
	}
	
	public interface DataChangeHandler{
		public void onFactoryDataChanged();
		public void onRecipeDataChanged();
		public void onPrototypeDataChanged();
		public void onMassProductionDataChanged();
	}

}
